﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowScreenshot
{
    public partial class Form1 : Form
    {
        Dictionary<int, string> Apps = null;
        List<int> pl = new List<int>();

        public Form1()
        {
            InitializeComponent();
        }

        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        private void GetWindowScreenshotByName(PictureBox PB)
        {
            IntPtr handle = IntPtr.Zero;
            try
            {
                PB.Image = null;
                handle = new IntPtr(pl[comboBox1.SelectedIndex]);
                //handle = FindWindow(null, winTitle);
                PB.Image = PrtWnd.PrtWindow(handle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            pl.Clear();
            try
            {
                Apps = PrtWnd.FindAllApps(this.Handle.ToInt32());
                foreach (string app in Apps.Values)
                    comboBox1.Items.Add(app);
                pl.AddRange(Apps.Keys);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetWindowScreenshotByName(pictureBox1);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                Apps = PrtWnd.FindAllApps(this.Handle.ToInt32());
                foreach (string app in Apps.Values)
                    comboBox1.Items.Add(app);
                pl.AddRange(Apps.Keys);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
